﻿/*-----------------------------------------------
// File: DropFireBall.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{
    public class DropFireBall : DropAngerBall 
    {
        public BattleCharacter targetBC
        {
            get
            {
                return causeBC;
            }
        }

        public float GetTotalTime()
        {
            return 2.5f;
        }

        public float tt = 0;
        public float left_tt = 0f;

        public override void Drop(BattleCharacter bc, BattleCharacter targetBC, float hpercent, Vector3 dir, Vector3 speed)
        {
            tt = 0f;
            base.Drop(bc, targetBC, hpercent, dir, speed);
        }

        public override void Drop(BattleCharacter bc, BattleCharacter targetBC) {
            tt = 0f;
            base.Drop(bc, targetBC);
        }

        protected override void DropTick()
        {
            if (isRun)
            {
                tt += Time.deltaTime;
                if (currentPhase == DropPhase.Circle_1)
                {
                    t += Time.deltaTime;
                    var newPos = curInitPosition + curInitSpeed * t + 0.5f * acceleration * t * t;
                    if (newPos.y <= 0.2)
                    {
                        currentPhase = DropPhase.Circle_2;
                        curInitPosition = transform.localPosition;
                        var curSpeed = curInitSpeed + acceleration * t;
                        curSpeed.y = -curSpeed.y;
                        curInitSpeed = curSpeed * 2 / 3;
                        t = 0f;
                        return;
                    }
                    transform.localPosition = newPos;
                    transform.Rotate(rotateXSpeed * Time.deltaTime, 0, 0);
                }
                else if (currentPhase == DropPhase.Circle_2)
                {
                    t += Time.deltaTime;
                    var newPos = curInitPosition + curInitSpeed * t + 0.5f * acceleration * t * t;
                    if (t >= 2 * curInitSpeed.y / g)
                    {
                        currentPhase = DropPhase.Circle_3;
                        curInitPosition = transform.localPosition;
                        var curSpeed = curInitSpeed + acceleration * t;
                        curSpeed.y = -curSpeed.y;
                        curInitSpeed = curSpeed * 2 / 3;
                        t = 0f;
                        return;
                    }
                    transform.localPosition = newPos;
                    transform.Rotate(rotateXSpeed * Time.deltaTime, 0, 0);
                }
                else if (currentPhase == DropPhase.Circle_3)
                {
                    t += Time.deltaTime;
                    var newPos = curInitPosition + curInitSpeed * t + 0.5f * acceleration * t * t;
                    if (t >= 2 * curInitSpeed.y / g)
                    {
                        currentPhase = DropPhase.Ground;
                        t = 0f;
                        curInitPosition = transform.localPosition;
                        curInitSpeed = Vector3.zero;
                        groundTime = 0f;
                        left_tt = GetTotalTime() - tt;
                        isStand = false;
                        return;
                    }
                    transform.localPosition = newPos;
                    transform.Rotate(rotateXSpeed * Time.deltaTime, 0, 0);
                }
                else if (currentPhase == DropPhase.Ground)
                {
                    t += Time.deltaTime;
                    var leftTime = left_tt - t;
                    if (leftTime > 0)
                    {
                        float leftDis = Vector3.Distance(transform.localPosition, targetBC.transform.localPosition);
                        Vector3 leftDir = targetBC.transform.localPosition + new Vector3(0, targetBC.height / 2, 0) - transform.localPosition;
                        leftDir = leftDir.normalized;
                        float leftSpeed = leftDis / leftTime;
                        var newPos = transform.localPosition + leftSpeed * leftDir * Time.deltaTime;
                        transform.localPosition = newPos;
                    }
                    else
                    {
                        transform.localPosition = targetBC.transform.localPosition + new Vector3(0, targetBC.height / 2, 0);
                    }
                }

                if (tt >= GetTotalTime())
                {
                    isRun = false;
                    Dispear();
                    return;
                }
            }
        }


        protected override void Dispear()
        {
            var obj = BattleFactory.SpawnDropFireBallDispear();
            obj.transform.SetParent(targetBC.transform);
            obj.transform.localPosition = new Vector3(0, targetBC.height / 2f, 0f);
            obj.transform.localScale = Vector3.one;
            obj.transform.localEulerAngles = Vector3.zero;
            BattleFactory.UnSpawn(gameObject);
            BattleFactory.UnSpawn(obj, 1.8f);
        }




    }
}
